package com.zyzy.leftfourdead.fragment.groupbuy;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.groupBuyAdapter.PreferenceRecyclerViewFragmentAdapter;
import com.zyzy.leftfourdead.adapter.groupBuyAdapter.PreferenceViewPagerFragmentAdapter;
import com.zyzy.leftfourdead.bean.PreferenceBean;
import com.zyzy.leftfourdead.helper.MyScrollView;
import com.zyzy.leftfourdead.helper.OkHttpClientHelper;
import com.zyzy.leftfourdead.helper.UrlHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by loverforever on 2016/8/24.
 * 团购的特惠团 碎片
 */
public class PreferenceFragment extends Fragment{
    //上下文对象
    private Context context;
    //特惠团装载广告的viewPager对象
    private ViewPager viewPager_preferenceFragment_groupBuy;
    //下面的recyclerView对象
    private RecyclerView recyclerView_preferenceFragment_groupBuy;
    //recyclerView的适配器
    private PreferenceRecyclerViewFragmentAdapter preferenceRecyclerViewFragmentAdapter;
    //viewPager的适配器
    private PreferenceViewPagerFragmentAdapter preferenceViewPagerFragmentAdapter;
    //定义PullToRefreshScrollView对象
   // private PullToRefreshScrollView scrollView_preferenceFragment_groupBuy;
    private MyScrollView scrollView_preferenceFragment_groupBuy;
    //定义Handler对象
    private Handler handler=new Handler();
    //定义装载PreferenceBean.BannersBean的集合 viewPager
    private List<PreferenceBean.BannersBean>bannersBeenList=new ArrayList<>();
    //定义装载PreferenceBean.ProductsBean的集合
    private List<PreferenceBean.ProductsBean>productsBeanList=new ArrayList<>();
    //当拉到最下面的时候 显示出来的文字
    private TextView textView_preferenceFragment_groupBuy;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取上下文对象
        context=getContext();
        //初始化适配器
        preferenceViewPagerFragmentAdapter=new PreferenceViewPagerFragmentAdapter(context,bannersBeenList);
        preferenceRecyclerViewFragmentAdapter=new PreferenceRecyclerViewFragmentAdapter(context,productsBeanList);
        //加载网络数据
        loadDataFromNet();
    }

    private void loadDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取特惠团的网址
                    String preferenceString= OkHttpClientHelper.getStringFromURL(context, UrlHelper.URL_PREFERENCE,"URL_PREFERENCE");
                    //解析json数据
                    PreferenceBean bannerBean=preferenceViewPagerFragmentAdapter.parseJsonToList(preferenceString);
                    bannersBeenList=bannerBean.getBanners();
                    productsBeanList=bannerBean.getProducts();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            preferenceViewPagerFragmentAdapter.reloadGridView(bannersBeenList,true);
                            preferenceRecyclerViewFragmentAdapter.reloadRecyclerView(productsBeanList,true);
                            //刷新
//                            scrollView_preferenceFragment_groupBuy.onRefreshComplete();
                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.preference_fragment,container,false);
//        scrollView_preferenceFragment_groupBuy= (PullToRefreshScrollView) view.findViewById(R.id.scrollView_preferenceFragment_groupBuy);
        scrollView_preferenceFragment_groupBuy= (MyScrollView) view.findViewById(R.id.scrollView_preferenceFragment_groupBuy);
        viewPager_preferenceFragment_groupBuy= (ViewPager) view.findViewById(R.id.viewPager_preferenceFragment_groupBuy);
        recyclerView_preferenceFragment_groupBuy= (RecyclerView) view.findViewById(R.id.recyclerView_preferenceFragment_groupBuy);
        textView_preferenceFragment_groupBuy= (TextView) view.findViewById(R.id.textView_preferenceFragment_groupBuy);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //设置适配器
        viewPager_preferenceFragment_groupBuy.setAdapter(preferenceViewPagerFragmentAdapter);
        recyclerView_preferenceFragment_groupBuy.setAdapter(preferenceRecyclerViewFragmentAdapter);
        //设置布局管理器
        recyclerView_preferenceFragment_groupBuy.setLayoutManager(new LinearLayoutManager(context));
//        scrollView_preferenceFragment_groupBuy.setMode(PullToRefreshBase.Mode.BOTH);
//        scrollView_preferenceFragment_groupBuy.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
//            @Override
//            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//                loadDataFromNet();
//            }
//
//            @Override
//            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//                textView_preferenceFragment_groupBuy.setText("没有更多了...");
//                scrollView_preferenceFragment_groupBuy.onRefreshComplete();
//            }
//        });

    }

    @Override
    //销毁
    public void onDestroy() {
        super.onDestroy();
        OkHttpClientHelper.cancelCall("URL_PREFERENCE");

    }
}
